package com.cql.sys.service.impl;

import com.cql.pojo.Detials;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.cql.pojo.Dict;
import com.cql.sys.mapper.DictMapper;
import com.cql.sys.service.DictService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Date 2025/6/17 11:19
 * @Created by 12336
 */

@Service
public class DictServiceImpl implements DictService {

    @Resource
    private DictMapper dictMapper;

    /**
     * 添加字典
     * @param dict
     * @return
     * @throws Exception
     */
    @Override
    public int addDict(Dict dict) throws Exception {
        return dictMapper.addDict( dict);
    }

    /**
     * 查询字典
     * @param dname
     * @return
     * @throws Exception
     */
    @Override
    public Dict findDict(String dname) throws Exception {
        return dictMapper.findDict( dname);
    }

    /**
     * 插入字典
     * @param dict
     * @return
     * @throws Exception
     */
    @Override
    public int insertDict(Dict dict) throws Exception {
        Dict db_dict = findDict(dict.getDname());
        return null == db_dict ? dictMapper.addDict(dict) : -1;
    }



    /**
     * 分页查询数据
     * @param pageNum
     * @param pageSize
     * @return
     */
        @Override
        public PageInfo<Dict> findDictByPage(Integer pageNum, Integer pageSize) {
            PageHelper.startPage(pageNum, pageSize);
            List<Dict> dictList = dictMapper.findAllDicts(); // 只查分页数据
            return new PageInfo<>(dictList); // 返回完整分页信息
        }

    @Override
    public int updateDict(Dict dict) throws Exception {
      return   dictMapper.updateDictById( dict);
    }

    @Override
    public Dict findDictById(Integer id) throws Exception {
            return dictMapper.findDictById( id);
    }

    @Override
    public int deleteDictById(int id) throws Exception {
            Dict res= dictMapper.findDictById(id);
            // 数据不存在 , 或者已经删除
            if (null ==res || res.getIsDel()== 1) return  -1 ;
            // 数据存在, 修改标志位
            res.setIsDel(1);
            return dictMapper.updateDictById( res);
    }

    @Override
    public List<Detials> findAllDetails(Dict dict ) throws Exception{
            return dictMapper.findDetials(dict.getDname());
    }


}
